/**
 * Copyright (C) 2011 KingCore Science & Technology CO,.LTD. All rights reserved.
 * XNS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/**
 * Copyright (C) 2002-2011 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * This software is the confidential and proprietary information of
 * WuZeven, Personal. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with WuZeven.
 */

package com.kingcore.director.resourcemaster.ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.Observable;
import java.util.ResourceBundle;

import javax.swing.JTabbedPane;
import javax.swing.UIManager;
import javax.swing.plaf.TabbedPaneUI;
import javax.swing.plaf.synth.PlainTabbedPaneUI;

import org.apache.log4j.Logger;

import wzw.util.ComponentUtil;
import wzw.util.I18nUtil;
import wzw.util.I18nUtils;

import com.kingcore.director.DirectorConstants;
import com.kingcore.director.base.ui.DirectorPanel;
import com.kingcore.director.core.util.DirectorComponentUtils;
import com.kingcore.director.main.MainFrame;

/**
 * <p>java class file describe</p>
 * @author Zeven on 2011-3-3
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @since	JDK5
 */

public class ResourceMasterMainPanel extends DirectorPanel implements ActionListener, MouseListener{

	//log object
	private final static Logger log = Logger.getLogger(ResourceMasterMainPanel.class);
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private MainFrame mainFrame = null;
	
	public ResourceMasterMainPanel(MainFrame mainFrame) {
		super(mainFrame); 
		
		this.mainFrame = mainFrame;
		initializeInterface();
		this.update(null, null);
	}

	public void refreshView(){
		// please do something in subclass.
		getMainTabbedPane().setSelectedIndex( currentTabIndex );
		DirectorPanel dPanle = (DirectorPanel) getMainTabbedPane().getSelectedComponent(); 
		dPanle.refreshView();
	}
	
	private void initializeInterface() {

		//
		this.setLayout(new BorderLayout());
		this.add( this.getMainTabbedPane(), BorderLayout.CENTER);
		
//		this.addComponentListener(new ComponentAdapter() {
//			public void componentShown(ComponentEvent e) {
//				log.debug("panel:shown");
//				refreshView();
//			}
//		});
	}
	
	private JTabbedPane mainTabPane = null;
	private int currentTabIndex = 0;  //MainTabPane_Index_Directory

	private FileCopyMainPanel fileCopyPanel;
	private FileExplorerMainPanel fileExplorerPanel;
	private FavoriteFilesMainPanel favoriteFilesPanel;
	private DocumentLibraryMainPanel documentLibraryPanel;
	private ProgramLibraryMainPanel programLibraryPanel;
	
	public final static int MainTabPane_FileExplorer_Index = 0;
	public final static int MainTabPane_FileCopy_Index = 1;
	public final static int MainTabPane_FavoriteFiles_Index = 2;
	public final static int MainTabPane_DocumentLibrary_Index = 3;
	public final static int MainTabPane_ProgramLibrary_Index = 4;

	public final static String MainTabPane_FileExplorer_Text = " File Explorer ";
	public final static String MainTabPane_FileCopy_Text = " File Copy "; //"<html> <u>C</u>opy File (Important)</html>";
	public final static String MainTabPane_FavoriteFiles_Text = " Favorite Files "; //<html> F<u>a</u>vorite Files (F<u>a</u>vorite)</html>";
	public final static String MainTabPane_DocumentLibrary_Text = " Document Library "; 
	public final static String MainTabPane_ProgramLibrary_Text = " Program Library "; 
	
//	private boolean isFavoriteFilesPanelAdded = false;
//	private boolean isFileCopyPanelAdded = false;
//	private boolean isProgramLibraryPanelAdded = false;
//	private boolean isDocumentLibraryPanelAdded = false;
	
	public JTabbedPane getMainTabbedPane() {
		if (mainTabPane == null) {
			mainTabPane = new JTabbedPane();
			if ( ComponentUtil.getNimbusLookAndFeelClassName().equals(
					UIManager.getLookAndFeel().getClass().getName() )  ) {
				mainTabPane.setUI(getTabbedPaneUI());
			}
			
			//if used more than two times,define a field but not a variable.
			mainTabPane.setTabPlacement(JTabbedPane.TOP);// 设定选项卡放在左边
			mainTabPane.addTab(MainTabPane_FileExplorer_Text, DirectorComponentUtils.createImageIcon(MainFrame.pic_fileExplorer), 
									getFileExplorerPanel(), null); //new JPanel()
			mainTabPane.addTab(MainTabPane_FileCopy_Text, DirectorComponentUtils.createImageIcon(MainFrame.pic_fileCopy),
									getFileCopyPanel(), null); //getCopyFilePanel()
			mainTabPane.addTab(MainTabPane_FavoriteFiles_Text, DirectorComponentUtils.createImageIcon(MainFrame.pic_favoriteFile), 
					getFavoriteFilesPanel(), null); // 
			mainTabPane.addTab(MainTabPane_DocumentLibrary_Text, DirectorComponentUtils.createImageIcon(MainFrame.pic_documentLibrary), 
					getDocumentLibraryPanel(), "网络云服务：文档库");
			mainTabPane.addTab(MainTabPane_ProgramLibrary_Text, DirectorComponentUtils.createImageIcon(MainFrame.pic_programLibrary), 
					getProgramLibraryPanelPanel(), "网络云服务：程序库");
			
			mainTabPane.addChangeListener(new javax.swing.event.ChangeListener() {
	            public void stateChanged(javax.swing.event.ChangeEvent e) {
	            	JTabbedPane   tabbedPane   =   (JTabbedPane)e.getSource();
	                int   selectedIndex   =   tabbedPane.getSelectedIndex(); 
               		log.info("tab stateChanged!");
               		currentTabIndex = selectedIndex;

	               	if(selectedIndex== MainTabPane_FileCopy_Index
	               			&& fileCopyPanel==null ){
	               		log.info("init Copy File Panel");
	               		//System.out.println("1--"+mainTabPane.getTitleAt(MainTabPane_FavoriteFiles_Index));
	               		mainTabPane.setComponentAt(MainTabPane_FileCopy_Index, getFileCopyPanel()); 
	               		//isFileCopyPanelAdded = true;
	               		//System.out.println("2--"+mainTabPane.getTitleAt(MainTabPane_FavoriteFiles_Index));
	               	
	               	}else if(selectedIndex== MainTabPane_FileExplorer_Index
	               			&& fileExplorerPanel==null ){
	               		log.info("init Directory Panel");
	               		
	               		mainTabPane.setComponentAt(MainTabPane_FileExplorer_Index, getFileExplorerPanel()); 
	               	
	               	}else if(selectedIndex== MainTabPane_FavoriteFiles_Index
	               			&& favoriteFilesPanel==null ){
	               		log.info("init Favorite Files Panel");
	               		mainTabPane.setComponentAt(MainTabPane_FavoriteFiles_Index, getFavoriteFilesPanel()); 
	               		//isFavoriteFilesPanelAdded = true;
	               		
	               	}else if(selectedIndex== MainTabPane_DocumentLibrary_Index
	               			&& documentLibraryPanel==null ){
	               		log.info("init Document Library Panel");
	               		mainTabPane.setComponentAt(MainTabPane_DocumentLibrary_Index, getDocumentLibraryPanel()); 
	               		//isDocumentLibraryPanelAdded = true;
	               	}else if(selectedIndex== MainTabPane_ProgramLibrary_Index
	               			&& programLibraryPanel==null ){
	               		log.info("init Program Library Panel");
	               		mainTabPane.setComponentAt(MainTabPane_ProgramLibrary_Index, getProgramLibraryPanelPanel()); 
	               		//isProgramLibraryPanelAdded = true;
	               		
	               	}
	               	DirectorPanel dPanel = (DirectorPanel) mainTabPane.getSelectedComponent();
	               	dPanel.refreshView();
	            }
	        });
		}
		return mainTabPane;
	}

	/**
	 * 自定义UI组件对象
	 */
	private TabbedPaneUI tabbedPaneUI = null;
	private TabbedPaneUI getTabbedPaneUI() {
		if (this.tabbedPaneUI==null) {
			this.tabbedPaneUI = new PlainTabbedPaneUI(13, -3); //加大Tab选项宽度，减小高度
		}
		return this.tabbedPaneUI;
	}

	private DocumentLibraryMainPanel getDocumentLibraryPanel() {
		if (documentLibraryPanel==null) {
			documentLibraryPanel = new DocumentLibraryMainPanel( this.mainFrame );
			this.mainFrame.getLanguageManager().addObserver(documentLibraryPanel);
		}
		return documentLibraryPanel;
	}

	private ProgramLibraryMainPanel getProgramLibraryPanelPanel() {
		if (programLibraryPanel==null) {
			programLibraryPanel = new ProgramLibraryMainPanel( this.mainFrame );
			this.mainFrame.getLanguageManager().addObserver(programLibraryPanel);
		}
		return programLibraryPanel;
	}
	
	public FileCopyMainPanel getFileCopyPanel() {
		if (fileCopyPanel==null) {
			fileCopyPanel = new FileCopyMainPanel( this.mainFrame );
			this.mainFrame.getLanguageManager().addObserver(fileCopyPanel);
		}
		return fileCopyPanel;
	}

	public FileExplorerMainPanel getFileExplorerPanel() {
		if (fileExplorerPanel==null) {
			fileExplorerPanel = new FileExplorerMainPanel(this.mainFrame);
			this.mainFrame.getLanguageManager().addObserver(fileExplorerPanel);
		}
		return fileExplorerPanel;
	}
	
	public FavoriteFilesMainPanel getFavoriteFilesPanel() {
		if (favoriteFilesPanel==null) {
			favoriteFilesPanel = new FavoriteFilesMainPanel( this.mainFrame );
			this.mainFrame.getLanguageManager().addObserver(favoriteFilesPanel);
		}
		return favoriteFilesPanel;
	}


	@Override
	 public void setStatusBarVisible(boolean isStatusBarShow) {
       	DirectorPanel dPanel = (DirectorPanel) mainTabPane.getSelectedComponent();
       	dPanel.setStatusBarVisible(isStatusBarShow);
	}

	@Override
	public void setToolBarVisible(boolean isToolBarShow) {
       	DirectorPanel dPanel = (DirectorPanel) mainTabPane.getSelectedComponent();
       	dPanel.setToolBarVisible(isStatusBarShow);
	} 

	
	/**
	 * <p>java method describe</p>
	 * @author Zeven on 2011-3-3
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	public int getCurrentTabIndex() {
		return currentTabIndex;
	}

	public void setCurrentTabIndex(int currentTabIndex) {
		this.currentTabIndex = currentTabIndex;
		this.getMainTabbedPane().setSelectedIndex(this.currentTabIndex); // add 
	}

	@Override
	public boolean fetchData() throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean saveData() throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void update(Observable o, Object arg) {
		//System.out.println(Locale.getDefault());
		ResourceBundle bundle = I18nUtils.getBundle(DirectorConstants.I18n_Language_Name);
		
		// Windows	    
		mainTabPane.setTitleAt(MainTabPane_FileExplorer_Index,
				I18nUtil.getBundleString(bundle, ResourceMasterMainPanel.class, "tab.FileExplorer.Text"));
		mainTabPane.setTitleAt(MainTabPane_FileCopy_Index,
				I18nUtil.getBundleString(bundle, ResourceMasterMainPanel.class, "tab.FileCopy.Text"));
		mainTabPane.setTitleAt(MainTabPane_FavoriteFiles_Index,
				I18nUtil.getBundleString(bundle, ResourceMasterMainPanel.class, "tab.FavoriteFiles.Text"));
		mainTabPane.setTitleAt(MainTabPane_DocumentLibrary_Index,
				I18nUtil.getBundleString(bundle, ResourceMasterMainPanel.class, "tab.DocumentLibrary.Text"));
		mainTabPane.setTitleAt(MainTabPane_ProgramLibrary_Index,
				I18nUtil.getBundleString(bundle, ResourceMasterMainPanel.class, "tab.ProgramLibrary.Text"));

		///((DirectorMenuBar)this.getJMenuBar()).update( o, arg );
	    ///this.getReminderMainPanel().update( o, arg );
	}

}
